package com.crocodileQ.common;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.annotation.JSONField;
import com.crocodileQ.enums.AppExceptionMsgEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * @Author crocodilePole
 * @param <T>
 */

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)// 属性为NULL 不序列化
public class R<T> {

    private int status;
//    private boolean success;
    private String message;
    private T data;

    @JSONField(format="yyyy-MM-dd HH:mm:ss")
    private DateTime time;

    /**
     * 正确result
     * @param data
     * @param <T>
     * @return
     */
    public static <T> R<T> success(T data){
        R<T> r = new R<>();
//        r.success = true;
        r.status = 200;
        r.data = data;
        r.message = "successful";
        r.time = DateTime.now();

        return r;
    }

    public static <T> R<T> success(String msg ,T data){
        R<T> r = new R<>();
//        r.success = true;
        r.status = 200;
        r.data = data;
        r.message = msg;
        r.time = DateTime.now();

        return r;
    }

    /**
     * 错误result
     * @param msg
     * @param status
     * @param <T>
     * @return
     */
    public static <T> R<T> error(String msg,int status){
        R<T> r = new R<>();
//        r.success = false;
        r.status = status;
        r.message = msg;
        r.time = DateTime.now();

        return r;
    }

    public static <T> R<T> error(AppExceptionMsgEnum appExceptionMsg){
        R<T> r = new R<>();
//        r.success = false;
        r.status = appExceptionMsg.getStatus();
        r.message = appExceptionMsg.getMsg();
        r.time = DateTime.now();

        return r;
    }


}
